package com.lh.project.controller;

import com.lh.project.common.BaseResponse;
import com.lh.project.common.ResultUtils;
import com.lh.project.common.UserHolder;
import com.lh.project.model.entity.Order;
import com.lh.project.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.lh.project.common.ErrorCode.SYSTEM_ERROR;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/post")
    public BaseResponse<String> setOrder(@RequestBody Order order){
        order.setOrderUserId(UserHolder.getUser().getId());
        System.out.println(order);
        boolean save = orderService.save(order);
        return save? ResultUtils.success("订单生成"):ResultUtils.error(SYSTEM_ERROR.getCode(),"订单生成失败");
    }

    @GetMapping("/get")
//    public BaseResponse<List<Order>> getOrderList(@RequestParam Long id){
    public BaseResponse<List<Order>> getOrderList(){
        Long id = UserHolder.getUser().getId();
        List<Order> list = orderService.getOrderList(id);
        return ResultUtils.success(list);
    }

}
